package com.mspbots.web.reports.impl;


import com.mspbots.web.config.RabbitConfiguration;
import com.mspbots.web.reports.MessageReportsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author sean.wang
 */
@Slf4j
@Service
public class MessageReportsServiceImpl implements MessageReportsService {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public void loginsRecordMessage(Long tenantId, String userName, String ip) {
        Map<String, Object> msg = new ConcurrentHashMap<>();
        msg.put("tenantId", tenantId);
        msg.put("userName", userName);
        msg.put("msgType", "Logins_record");
        msg.put("msgDatetime", LocalDateTime.now().toString());
        msg.put("ip", ip);
        rabbitTemplate.convertAndSend(RabbitConfiguration.EXCHANGE_NAME, RabbitConfiguration.LOGINS_RECORD_QUEUE, msg);
    }

    @Override
    public void pageViewsMessage(Long tenantId, String userName, String uri, String ip) {
        Map<String, Object> msg = new ConcurrentHashMap<>();
        msg.put("tenantId", tenantId);
        msg.put("userName", userName);
        msg.put("uri", uri);
        msg.put("msgType", "Pageviews");
        msg.put("msgDatetime", LocalDateTime.now().toString());
        msg.put("ip", ip);
        rabbitTemplate.convertAndSend(RabbitConfiguration.EXCHANGE_NAME, RabbitConfiguration.PAGEVIEWS_QUEUE, msg);
    }
}
